package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

//func Router(){
//	r := gin.Default()
//
//
//}

const (
	messageInternalServerError     = "服务内部错误，请联系管理员"
	messageUsernameOrPasswordError = "用户名或密码不正确"
	messageLoginSuccess            = "登录成功"
	messageRequiredLogin           = "您未登录或者您的登录已过期，请重新登录"
	messageLogoutSuccess           = "退出登录成功"
	messageSuccess                 = "操作成功"
	messageBadRequest              = "请求参数不正确"
	messageNotFound                = "资源不存在"
	messageEmailError              = "邮箱格式不正确"
	messageRequiredInput           = "请输入必填项"
	messageNotEqualTwicePassword   = "两次输入密码不一致"
	messageMustLogin               = "内容暂时不允许游客访问，请先登录"
	messageForbidRegister          = "网站已经关闭了注册功能，暂时不允许注册"
	maxPageSize                    = 30
)

var (
	BaseAuth Auth
	//微信小程序支持的 HTML 标签：https://developers.weixin.qq.com/miniprogram/dev/component/rich-text.html
	weixinTags = []string{"a", "abbr", "address", "article", "aside", "b", "bdi", "bdo", "big", "blockquote", "br", "caption", "center", "cite", "code", "col", "colgroup", "dd", "del", "div", "dl", "dt", "em", "fieldset", "font", "footer", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hr", "i", "img", "ins", "label", "legend", "li", "mark", "nav", "ol", "p", "pre", "q", "rt", "ruby", "s", "section", "small", "span", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", " thead", "tr", "tt", "u", "ul"}
	appTags    = []string{"a", "abbr", "b", "blockquote", "br", "code", "col", "colgroup", "dd", "del", "div", "dl", "dt", "em", "fieldset", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hr", "i", "img", "ins", "label", "legend", "li", "ol", "p", "q", "span", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "tt", "ul"}
)

type (
	Auth struct {
		Token   string `json:"token"`
		Version string `json:"version"`
	}

	APIResponse struct {
		Msg  string      `json:"message"`
		Data interface{} `json:"data"`
	}
)

func Response(c *gin.Context, httpStatus int, message string, data interface{}) {
	c.JSON(httpStatus, APIResponse{
		Msg:  message,
		Data: data,
	})
}

// 处理跨域请求,支持options访问
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		c.Header("Access-Control-Allow-Origin", origin)
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id")
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")

		// 放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		// 处理请求
		c.Next()
	}
}

//对特定的 URL鉴权
func Authority() gin.HandlerFunc {
	return func(c *gin.Context) {

	}
}

type responseResult struct {
	Code int         `json:"code" form:"code"`
	Msg  string      `json:"message" form:"message"`
	Data interface{} `json:"data" form:"data"`
}

func Ok(c *gin.Context, data interface{}) {
	c.JSON(200, responseResult{
		Code: 200,
		Msg:  "success",
		Data: data,
	})
}

func Fail(c *gin.Context, code int, msg string, data ...interface{}) {
	c.JSON(200, responseResult{
		Code: code,
		Msg:  msg,
		Data: data,
	})
	c.Abort()
}
